<script setup lang="ts">
import { bindMobileToLogin, loginByQQ } from '@/services/user'
import { codeRule, mobileRule } from '@/utils/validate'
import { ref } from 'vue'
import { useCountDown } from '@/composables'
import { useStore } from '@/stores'
import { useRouter } from 'vue-router'
import { showSuccessToast } from 'vant'
import { useRoute } from 'vue-router'
// 获取openId 并携带校验是否在数据库中有记录
// 检测是否三方登录成功
// console.log(QC.Login.check())
const isNeedBind = ref(false)
const { user } = useStore()
const router = useRouter()
const route = useRoute()

if (window.QC.Login.check()) {
  window.QC.Login.getMe(async (id: string) => {
    console.log(id) // openId
    openId.value = id
    //  携带openId校验是否在数据库中有记录
    try {
      const res = await loginByQQ({ openId: id, source: 'qq' })
      // 2. 提示
      showSuccessToast('三方登录成功')
      // 3. 存储个人信息
      user.setUserInfo(res.data)
      // 4. 跳转
      user.returnURL
        ? router.push(user.returnURL as string)
        : router.push('/user')
      // 5. 扫码登录成功后最好将returnURL清空
      user.setReturnURL('')
    } catch {
      isNeedBind.value = true
      // console.log('绑定手机号后登录')
    }
  })
}
const openId = ref('')
const mobile = ref('')
const code = ref('')
const { formRef, getCode, time } = useCountDown(mobile, 'bindMobile')
const submit = async () => {
  // 1. 绑定手机号
  const res = await bindMobileToLogin({
    code: code.value,
    mobile: mobile.value,
    openId: openId.value
  })
  // 2. 提示
  showSuccessToast('绑定登录成功')
  // 3. 存储个人信息
  user.setUserInfo(res.data)
  // 4. 跳转
  user.returnURL ? router.push(user.returnURL as string) : router.push('/user')
  // 5. 扫码登录成功后最好将returnURL清空
  user.setReturnURL('')
}
</script>
<template>
  <div class="login-page" v-if="isNeedBind">
    <cp-nav-bar></cp-nav-bar>
    <div class="login-head">
      <h3>手机绑定</h3>
    </div>
    <van-form autocomplete="off" ref="formRef" @submit="submit">
      <van-field
        name="mobile"
        placeholder="请输入手机号"
        v-model="mobile"
        :rules="mobileRule"
      ></van-field>
      <van-field
        name="code"
        placeholder="请输入验证码"
        v-model="code"
        :rules="codeRule"
      >
        <template #button>
          <span class="btn-send" :class="{ active: time }" @click="getCode">{{
            time ? `${time}s后发送` : '发送验证码'
          }}</span>
        </template>
      </van-field>
      <div class="cp-cell">
        <van-button
          style="margin-top: 50px"
          block
          round
          type="primary"
          native-type="submit"
        >
          立即绑定
        </van-button>
      </div>
    </van-form>
  </div>
</template>

<style lang="scss" scoped>
.login {
  &-page {
    padding-top: 46px;
  }
  &-head {
    display: flex;
    padding: 30px 30px 50px;
    justify-content: space-between;
    align-items: flex-end;
    line-height: 1;
    h3 {
      font-weight: normal;
      font-size: 24px;
    }
    a {
      font-size: 15px;
    }
  }
  &-other {
    margin-top: 60px;
    padding: 0 30px;
    .icon {
      display: flex;
      justify-content: center;
      img {
        width: 36px;
        height: 36px;
        padding: 4px;
      }
    }
  }
}
.van-form {
  padding: 0 14px;
  .cp-cell {
    height: 52px;
    line-height: 24px;
    padding: 14px 16px;
    box-sizing: border-box;
    display: flex;
    align-items: center;
    .van-checkbox {
      a {
        color: var(--cp-primary);
        padding: 0 5px;
      }
    }
  }
  .btn-send {
    color: var(--cp-primary);
    &.active {
      color: rgba(22, 194, 163, 0.5);
    }
  }
}
</style>

<style scoped></style>
